<template>
  <view class="com-page">
    <view class="choose" @click="choose()"> </view>

    <uni-popup ref="pop" :mask-click="false"></uni-popup>
  </view>
</template>
<script setup lang="ts">
/**
 * 模块引入
 */
import { reactive, getCurrentInstance, onMounted, ref } from "vue";
const { proxy } = getCurrentInstance();
/**
 * 接口
 */
import { uploadFile, checkFileExist } from "@/apis/common.ts";
/**
 * 父组件数据
 */
const props = defineProps({
  picList: {
    type: Array,
    default: [],
  },
  limit: {
    type: Number,
    default: 1,
  },
  isVideo: {
    type: Boolean,
    default: false,
  },
  index: {
    type: Number,
    default: -1,
  },
});
/**
 * 数据
 */
const data = reactive({
  url: [],
});
/**
 * 初始方法
 */
onMounted(() => {});
/**
 * 功能方法
 */
const choose = () => {
  // #ifdef MP-WEIXIN
  chooseMedia();
  // #endif
  // #ifndef MP-WEIXIN
  otherPlatformChoose();
  // #endif
};
const chooseMedia = () => {
  uni.chooseMedia({
    count: props.limit,
    mediaType: [props.isVideo ? "video" : "image"],
    sourceType: ["album"],
    maxDuration: 30,
    sizeType: ["compressed"],
    success(res: any) {
      pop.value.open();
      uni.showLoading({
        title: "正在上传",
      });
      handleMedia(res.tempFiles[0].tempFilePath);
    },
  });
};
const otherPlatformChoose = () => {
  if (props.isVideo) {
    uni.chooseVideo({
      sourceType: ["album"],
      success(res: any) {
        pop.value.open();
        uni.showLoading({
          title: "正在上传",
        });
        console.log(res);
        handleMedia(res.tempFilePath);
      },
    });
  } else {
    uni.chooseImage({
      count: props.limit, //默认9
      sizeType: ["compressed"], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album"], //从相册选择
      success(res: any) {
        pop.value.open();
        uni.showLoading({
          title: "正在上传",
        });
        handleMedia(res.tempFiles[0].tempFilePath);
      },
    });
  }
};
const handleMedia = async (url: any) => {
  await upload(url);
  pop.value.close();
  uni.hideLoading();
  if (props.index == -1) {
    emit("success", data.url);
  } else {
    emit("success", data.url, props.index);
  }
};

const upload = async (url: any) => {
  return new Promise((resolve: any) => {
    uni.uploadFile({
      url: `${proxy.$baseUrl.baseUrl}/file/uploadMd5`,
      name: "file",
      filePath: url,
      formData: {
        path: "video",
      },
      success: (res: any) => {
        if (res.data) {
          let response = JSON.parse(res.data);
          data.url = response.data;

          resolve(data.url);
        }
      },
      fail: () => {
        pop.value.close();
        uni.hideLoading();
        setTimeout(() => {
          uni.showToast({
            title: "上传失败",
            icon: "none",
          });
        }, 1000);
      },
    });
  });
};

/**
 * 父组件方法
 * const emit = defineEmits(['function']);
 * emit('function', data);
 */
const emit = defineEmits(["success"]);

/**
 * 父组件传递生命周期方法
 * onShareAppMessage  分享好友
 * onShareTimeline 分享朋友圈
 * onReachBottom 页面内触底
 */

/**
 * 子组件方法
 */
const pop = ref();
</script>
<style lang="scss" scoped>
@import url("@/static/css/theme.scss");
.com-page {
  position: absolute;
  width: 100%;
  height: 100%;
  .choose {
    width: 100%;
    height: 100%;
  }
}
</style>
